import {
  IsNotEmpty,
  IsString,
  MaxLength,
  IsOptional,
  IsNumber,
  IsEnum,
  Min,
  Max,
} from 'class-validator';
import { Transform } from 'class-transformer';
import { ApiProperty } from '@nestjs/swagger';
import { NovelStatus } from '../entities/novel.entity';

export class CreateNovelDto {
  @ApiProperty({ description: '小说标题', example: '斗破苍穹' })
  @IsNotEmpty({ message: '小说标题不能为空' })
  @IsString({ message: '小说标题必须是字符串' })
  @MaxLength(200, { message: '小说标题最多200个字符' })
  title: string;

  @ApiProperty({ description: '作者', example: '天蚕土豆' })
  @IsNotEmpty({ message: '作者不能为空' })
  @IsString({ message: '作者必须是字符串' })
  @MaxLength(100, { message: '作者最多100个字符' })
  author: string;

  @ApiProperty({
    description: '小说简介',
    example: '这里是小说简介...',
    required: false,
  })
  @IsOptional()
  @IsString({ message: '小说简介必须是字符串' })
  description?: string;

  @ApiProperty({
    description: '封面图片URL',
    example: 'https://example.com/cover.jpg',
    required: false,
  })
  @IsOptional()
  @IsString({ message: '封面图片URL必须是字符串' })
  @MaxLength(255, { message: '封面图片URL最多255个字符' })
  coverImage?: string;

  @ApiProperty({ description: '母母分类ID', example: 1 })
  @IsOptional()
  @IsNumber({}, { message: '母母母分类ID必须是数字' })
  masterCategoryId?: number;

  @ApiProperty({
    description: '小说状态',
    example: '1',
    enum: NovelStatus,
    required: false,
  })
  @IsOptional()
  @IsEnum(NovelStatus, { message: '小说状态值不正确' })
  status?: NovelStatus;

  @ApiProperty({ description: '总字数', example: 1000000, required: false })
  @IsOptional()
  @IsNumber({}, { message: '总字数必须是数字' })
  @Min(0, { message: '总字数不能小于0' })
  wordCount?: number;

  @ApiProperty({ description: '评分', example: 4.5, required: false })
  @IsOptional()
  @IsNumber({}, { message: '评分必须是数字' })
  @Min(0, { message: '评分不能小于0' })
  @Max(5, { message: '评分不能大于5' })
  ratingScore?: number;
}

export class UpdateNovelDto {
  @ApiProperty({
    description: '小说标题',
    example: '斗破苍穹',
    required: false,
  })
  @IsOptional()
  @IsString({ message: '小说标题必须是字符串' })
  @MaxLength(200, { message: '小说标题最多200个字符' })
  title?: string;

  @ApiProperty({ description: '作者', example: '天蚕土豆', required: false })
  @IsOptional()
  @IsString({ message: '作者必须是字符串' })
  @MaxLength(100, { message: '作者最多100个字符' })
  author?: string;

  @ApiProperty({
    description: '小说简介',
    example: '这里是小说简介...',
    required: false,
  })
  @IsOptional()
  @IsString({ message: '小说简介必须是字符串' })
  description?: string;

  @ApiProperty({
    description: '封面图片URL',
    example: 'https://example.com/cover.jpg',
    required: false,
  })
  @IsOptional()
  @IsString({ message: '封面图片URL必须是字符串' })
  @MaxLength(255, { message: '封面图片URL最多255个字符' })
  coverImage?: string;

  @ApiProperty({ description: '母分类ID', example: 1, required: false })
  @IsOptional()
  @IsNumber({}, { message: '母母分类ID必须是数字' })
  masterCategoryId?: number;

  @ApiProperty({
    description: '小说状态',
    example: '1',
    enum: NovelStatus,
    required: false,
  })
  @IsOptional()
  @IsEnum(NovelStatus, { message: '小说状态值不正确' })
  status?: NovelStatus;

  @ApiProperty({ description: '总字数', example: 1000000, required: false })
  @IsOptional()
  @IsNumber({}, { message: '总字数必须是数字' })
  @Min(0, { message: '总字数不能小于0' })
  wordCount?: number;

  @ApiProperty({ description: '评分', example: 4.5, required: false })
  @IsOptional()
  @IsNumber({}, { message: '评分必须是数字' })
  @Min(0, { message: '评分不能小于0' })
  @Max(5, { message: '评分不能大于5' })
  ratingScore?: number;
}

export class NovelQueryDto {
  @ApiProperty({ description: '页码', example: 1, required: false })
  @IsOptional()
  @Transform(({ value }) => parseInt(value))
  @IsNumber({}, { message: '页码必须是数字' })
  @Min(1, { message: '页码不能小于1' })
  page?: number = 1;

  @ApiProperty({ description: '每页数量', example: 10, required: false })
  @IsOptional()
  @Transform(({ value }) => parseInt(value))
  @IsNumber({}, { message: '每页数量必须是数字' })
  @Min(1, { message: '每页数量不能小于1' })
  @Max(100, { message: '每页数量不能大于100' })
  limit?: number = 10;

  @ApiProperty({ description: '母分类ID', example: 1, required: false })
  @IsOptional()
  @Transform(({ value }) => parseInt(value))
  @IsNumber({}, { message: '母母分类ID必须是数字' })
  masterCategoryId?: number;

  @ApiProperty({ description: '搜索关键词', example: '斗破', required: false })
  @IsOptional()
  @IsString({ message: '搜索关键词必须是字符串' })
  keyword?: string;

  @ApiProperty({ description: '排序方式', example: 'latest', required: false })
  @IsOptional()
  @IsString({ message: '排序方式必须是字符串' })
  sort?: string = 'latest';

  @ApiProperty({
    description: '小说状态',
    example: '1',
    enum: NovelStatus,
    required: false,
  })
  @IsOptional()
  @IsEnum(NovelStatus, { message: '小说状态值不正确' })
  status?: NovelStatus;
}
